package org.jeecg.modules.flow.callback.delivery.move;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.RequiredArgsConstructor;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.business.outbound.entity.WmsOutReceiptTaskMaterial;
import org.jeecg.modules.business.outbound.service.IWmsOutReceiptTaskMaterialService;
import org.jeecg.modules.business.repertory.entity.vo.RepertoryDocumentVO;
import org.jeecg.modules.business.repertory.entity.vo.RepertoryVO;
import org.jeecg.modules.business.repertory.service.IWmsRepertoryService;
import org.jeecg.modules.business.transfer.entity.WmsMoveReceipt;
import org.jeecg.modules.business.transfer.entity.WmsMoveReceiptMaterial;
import org.jeecg.modules.business.transfer.service.IWmsMoveReceiptMaterialService;
import org.jeecg.modules.business.transfer.service.IWmsMoveReceiptService;
import org.jeecg.modules.business.transfer.vo.WmsMoveReceiptPage;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTaskMaterial;
import org.jeecg.modules.flow.callback.enums.DocumentStatusEnum;
import org.jeecg.modules.flow.callback.move.util.CommonMoveUtil;
import org.jeecg.modules.flow.sys.common.BaseCustomBeanRun;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 条料调拨因出移库单 备料
 *
 * @author gongwenkai
 * @version 1.0.0
 * @since 2023-11-27
 */
@RequiredArgsConstructor
@Component
public class Y106MoveReceiptPrepareBean<T> extends BaseCustomBeanRun<T> {

    private final IWmsMoveReceiptService wmsMoveReceiptService;

    private final IWmsMoveReceiptMaterialService wmsMoveReceiptMaterialService;

    private final IWmsOutReceiptTaskMaterialService wmsOutReceiptTaskMaterialService;

    private final IWmsRepertoryService wmsRepertoryService;

    private final CommonMoveUtil commonMoveUtil;

    @Override
    public void actureRun() {
        BaseFlowDto baseFlowDto = this.getBaseFlowDto();
        baseFlowDto.translateClass(WmsMoveReceiptPage.class);
        WmsMoveReceiptPage wmsMoveReceiptPage = (WmsMoveReceiptPage) baseFlowDto.getData();
        String receiptId = wmsMoveReceiptPage.getId();
        WmsMoveReceipt wmsMoveReceipt = new WmsMoveReceipt();
        List<WmsMoveReceiptMaterial> wmsMoveReceiptMaterialList=new ArrayList<>();
        if (StrUtil.isEmpty(receiptId)) {
            //需要先保存移库单
            BeanUtils.copyProperties(wmsMoveReceiptPage, wmsMoveReceipt);
            receiptId = wmsMoveReceiptService.saveMain(wmsMoveReceipt, wmsMoveReceiptPage.getWmsMoveReceiptMaterialList());
            //提交出库单
            wmsRepertoryService.moveSubmit(receiptId);
            //修改实时库存
            wmsMoveReceiptMaterialList = wmsMoveReceiptPage.getWmsMoveReceiptMaterialList();
            commonMoveUtil.moveRepertory(receiptId,wmsMoveReceipt.getTransferFromWarehouseId(),wmsMoveReceiptMaterialList);

        } else {
            //提交出库单
            wmsRepertoryService.moveSubmit(receiptId);
            wmsMoveReceipt = wmsMoveReceiptService.getById(receiptId);
            if(Objects.isNull(wmsMoveReceipt)){
                throw new JeecgBootException("移库单id错误");
            }
            //修改实时库存
            wmsMoveReceiptMaterialList = wmsMoveReceiptMaterialService.list(new LambdaQueryWrapper<WmsMoveReceiptMaterial>()
                    .eq(WmsMoveReceiptMaterial::getWmsMoveReceiptId, receiptId)
            );
            commonMoveUtil.moveRepertory(receiptId,wmsMoveReceipt.getTransferFromWarehouseId(), wmsMoveReceiptMaterialList);

        }
        // 更新移库单的单据状态
        wmsMoveReceiptService.update(new LambdaUpdateWrapper<WmsMoveReceipt>()
                .set(WmsMoveReceipt::getDocumentStatus, DocumentStatusEnum.COMMIT.getCode())
                .eq(WmsMoveReceipt::getId,receiptId)
        );
        // 更新出库任务物料明细的已移库字段
        for(WmsMoveReceiptMaterial moveReceiptMaterial:wmsMoveReceiptMaterialList){
            wmsOutReceiptTaskMaterialService.update(new LambdaUpdateWrapper<WmsOutReceiptTaskMaterial>()
                    .set(WmsOutReceiptTaskMaterial::getHasMoved,1)
                    .eq(WmsOutReceiptTaskMaterial::getWmsOutReceiptTaskId,wmsMoveReceipt.getDeliveryOutTaskId())
                    .eq(WmsOutReceiptTaskMaterial::getBatchCode,moveReceiptMaterial.getBatchCode())
                    .eq(WmsOutReceiptTaskMaterial::getWmsBasicMaterialId,moveReceiptMaterial.getWmsBasicMaterialId())
                    .eq(WmsOutReceiptTaskMaterial::getWmsBasicWarehouseId,wmsMoveReceipt.getTransferFromWarehouseId())
            );
        }
        baseFlowDto.setInfoId(receiptId);
        baseFlowDto.setResult(Result.OK());
    }
}
